package pv.finder;

import java.util.List;

import pv.dto.AbstractDTO;
import pv.dto.DistrictDTO;
import pv.dto.PlaceDTO;
import pv.finder.search.DistrictSearch;
import pv.map.MapHelper;
import pv.util.DataSearch;
import pv.util.Utility;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.SystemClock;
import android.provider.Settings.System;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.google.android.gms.maps.MapView;

public class FMLoading extends Fragment implements View.OnClickListener {
	protected DataHelper dataHelper;

	// protected List<PlaceDTO> list;

	public FMLoading() {

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fm_loading, null);

		// child on view

		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		dataHelper = new DataHelper(getActivity());
		if (!dataHelper.existsDatabase()) {
			new SyncData(dataHelper, getActivity()).execute("");
		} else {
			new LoadAddressInfo(dataHelper, getActivity()).execute("");
		}

	}

	@Override
	public void onClick(View v) {

		// TODO Auto-generated method stub
		if (v.getId() == R.id.btnRetry) {
			v.setVisibility(View.GONE);
			((ProgressBar) getView().findViewById(R.id.pbloading))
					.setVisibility(View.VISIBLE);
			new SyncData(dataHelper, getActivity()).execute("");
		}

	}

	class LoadAddressInfo extends DataHelper.LoadDataForAddress {

		public LoadAddressInfo(DataHelper dataHelper, Context context) {

			dataHelper.super(context);
			// TODO Auto-generated constructor stub
		}

		@Override
		protected void showLoadingDialog() {
			// TODO Auto-generated method stub
		}

		@Override
		protected void dismissDialog() {
			// TODO Auto-generated method stub
		}

		@Override
		protected void onProgressUpdate(Object... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);
		}

		@Override
		protected void doIfSuccess() {
			// TODO Auto-generated method stub

			Utility.goToFragment((FragmentActivity) context, new FMMap(),
					getString(R.string.fmmap));

		}
	}

	class SyncData extends DataHelper.SyncDataFromServer {

		public SyncData(DataHelper dataHelper, Context context) {
			dataHelper.super(context);
			// TODO Auto-generated constructor stub
		}

		@Override
		protected void doIfSuccess() {
			// TODO Auto-generated method stub
			new LoadAddressInfo(dataHelper, context).execute("");
		}

		@Override
		protected void showLoadingDialog() {
			// TODO Auto-generated method stub
		}

		@Override
		protected void dismissDialog() {
			// TODO Auto-generated method stub
		}

		@Override
		protected void doIfErrorNetwork() {
			// TODO Auto-generated method stub
			((Button) getView().findViewById(R.id.btnRetry))
					.setOnClickListener(FMLoading.this);
			((Button) getView().findViewById(R.id.btnRetry))
					.setVisibility(View.VISIBLE);
			((ProgressBar) getView().findViewById(R.id.pbloading))
					.setVisibility(View.GONE);
			super.doIfErrorNetwork();
		}
	}

}
